package com.f21.f21assessoria.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;

import com.f21.f21assessoria.enums.EnumConverter;

public abstract class TemplateConverterEnum implements Converter {

	public abstract Object getAsObject(FacesContext context,
			UIComponent component, String value);

	public String getAsString(FacesContext context, UIComponent component,
			Object value) {				
		if(value instanceof List<?>){
			String separador = "";
			StringBuffer stringBuffer = new StringBuffer();
			for( EnumConverter en : (List<EnumConverter>) value){
				stringBuffer.append(separador);
				stringBuffer.append(en.getLabel());
				separador = ", ";
			}
			return stringBuffer.toString();
		}
		return ((EnumConverter) value).getLabel();
	}

	public static SelectItem[] getListaValores(EnumConverter[] list) {
		SelectItem[] lista = new SelectItem[list.length];
		int i = 0;
		for (EnumConverter e : list) {
			lista[i++] = new SelectItem(e, e.getLabel());
		}
		return lista;
	}

	public static SelectItem[] getListaValores(EnumConverter[] list,
			String inicio) {
		SelectItem[] lista = new SelectItem[list.length + 1];
		int i = 0;
		lista[i++] = new SelectItem("", inicio);
		for (EnumConverter e : list) {
			lista[i++] = new SelectItem(e, e.getLabel());
		}
		return lista;
	}

}
